/**
 * Created by lamb
 * Time: 11-12-5 下午1:35
 */
var http = require('http');
function get_show_logout(path, callback) {
    var options = {
        host: 'www.moko.cc',
        port: 80,
        path: path,
        method: 'GET'
    };

    var req = http.request(options, function(res) {
        //console.log('STATUS: ' + res.statusCode);
        //console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        var buffers = [], size = 0;
        res.on('data', function (buffer) {
            buffers.push(buffer);
            size += buffer.length;
        });
        res.on('end', function() {
            var html = buffers.toString();

            var title_start = html.indexOf(">", html.indexOf('<a id="blog_content')) + 1;
            var title_end = html.indexOf('<', title_start);
            var title = html.substring(title_start, title_end);

            var article_start = html.lastIndexOf('<div class="article" id="postContentDiv">');
            var article_end = html.indexOf('<script>jQuery1.mk.face.formatImages("postContentDiv");</script>');
            var article_html = html.substring(article_start, article_end);
            var more_start = html.indexOf('<div class="sElse dBd cf" id="zuoYouPostListDiv">');
            var more_end = html.indexOf('<div id="divnotShowComment" class="plTips dBd_1 gC">');
            var more_html = html.substring(more_start, more_end);
            //pics
            var pic_start = article_html.indexOf('<div class="pic dBd">');
            var pic_end = article_html.lastIndexOf('<div class="dBd">');
            article_html = article_html.substring(pic_start, pic_end);
            while (article_html.indexOf(",") != -1) {
                article_html = article_html.replace(",", "");
            }
            //console.log(html);
            var pics = [];
            var separate = '<div class="pic dBd">';
            var quotation_mark = '"';
            var src2_separate = 'src2="';


            while (article_html.indexOf(separate) != -1) {

                var item_start = article_html.lastIndexOf(separate);
                var item_html = article_html.substring(item_start);
                article_html = article_html.substring(0, item_start);

                var src2_start = item_html.indexOf(src2_separate) + src2_separate.length;
                var src2_end = item_html.indexOf(quotation_mark, src2_start);
                var src2 = item_html.substring(src2_start, src2_end);
                pics.unshift(src2);
            }
            //more
            var more_items = new Array();
            var show_small_start = more_html.indexOf('<div class="show small">');
            var show_small_end = more_html.lastIndexOf('<a class="arrow6"');
            more_html = more_html.substring(show_small_start, show_small_end);
            while (more_html.indexOf(",") != -1) {
                more_html = more_html.replace(",", "");
            }
            var separate_more = '<div class="show small">';
            var href_separate = '<a class="coverBg wC" href="';
            var src_separate = '<img class="cover" src="';
            while (more_html.indexOf(separate_more) != -1) {
                var more_item = new Object();
                var more_item_start = more_html.lastIndexOf(separate_more);
                var more_item_html = more_html.substring(more_item_start);
                more_html = more_html.substring(0, more_item_start);

                var href_start = more_item_html.indexOf(href_separate) + href_separate.length;
                var href_end = more_item_html.indexOf(quotation_mark, href_start);
                var href = more_item_html.substring(href_start, href_end);
                var href_s = href.indexOf("/", "/post/".length);
                var href_e = href.indexOf("/", href_s+1);
                more_item.href = href.substring(0, href_s) + href.substr(href_e);

                var src_start = more_item_html.indexOf(src_separate) + src_separate.length;
                var src_end = more_item_html.indexOf(quotation_mark, src_start);
                more_item.src = more_item_html.substring(src_start, src_end);
                if (more_item.href != options.path) {
                    more_items.unshift(more_item);
                }
            }

            var object = new Object();
            object.title = title;
            object.pics = pics;
            object.more = more_items;
            //console.log(object);
            //console.log(pics.length);
            callback(object);

        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    // write data to request body
    //req.write('data\n');
    //req.write('data\n');
    req.end();
}
exports.get_show_logout = get_show_logout;